import utils from "../../utils/util";

let app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        loadingHidden: true,
        isStu: true,
        id: '',
        type: '',
        collectionList: []
    },
    initPage() {
        utils.mask(this)
        // 获取用户 没有的话前往登录
        if (app.globalData.userType === -1) {
            utils.redirectToPageAndDistroy('/pages/login/login', this)
        } else {
            console.log(app.globalData.userType === 1)
            this.setData({
                isStu: (app.globalData.userType === 1),
                id: app.globalData.userInfo.id
            })
            this.loadMyMsgInfo()
        }
    },

    tosend() {
        let url
        console.log(this.data.type)
        if (this.data.type === '3') {
            url = '/pages/sendjob/sendjob'
        } else {
            url = '/pages/sendmsg/sendmsg'
        }
        utils.redirectToPage(url, this)
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log("当前信息类型：", options.type)
        if (options.type) {
            this.setData({
                type: options.type
            })
        }
        this.initPage()

    },

    loadMyMsgInfo() {
        let that = this
        utils.mask(that)
        let data = {}
        data.id = this.data.id
        if (app.globalData.userType === 1) {
            data.studentId = app.globalData.userInfo.id
        } else {
            data.shopId = app.globalData.userInfo.id
        }
        if (this.data.type) {
            data.type = this.data.type
        }
        wx.request({
            url: app.globalData.serviceUrl + '/message/listByParam',
            data: data,
            success: function (res) {
                let tempData = res.data
                if (tempData.success) {
                    that.setData({
                        collectionList: tempData.data
                    })
                } else {
                    utils.toast(tempData.msg)
                }
                utils.removeMask(that)
            },
            fail: function () {
                utils.toast('请求发送失败')
                utils.removeMask(that)
            }
        })
    },
    showCollInfo(e) {
        let id = e.currentTarget.dataset.mid;
        console.log(id)
        utils.redirectToPage('/pages/message/message?id=' + id, this)
    },

    deleteColl(e) {
        let that = this
        let id = e.currentTarget.dataset.id;
        console.log(id)
        wx.showModal({
            title: '提示',
            content: '删除后不可恢复，是否继续?',
            success(res) {
                if (res.confirm) {
                    utils.mask(that)
                    wx.request({
                        url: app.globalData.serviceUrl + '/message/delete',
                        data: {
                            id: id
                        },
                        success: function (res) {
                            let tempData = res.data
                            if (tempData.success) {
                                utils.toast('删除成功')
                                that.loadMyMsgInfo()
                            } else {
                                utils.toast(tempData.msg)
                            }
                            utils.removeMask(that)
                        },
                        fail: function () {
                            utils.removeMask(that)
                            utils.toast('请求失败')
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })


    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.loadMyMsgInfo()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.loadMyMsgInfo()
        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
